package ${package.BuilderPackage};

import ${package.ReqVoPackage}.*;
import ${package.RespVoPackage}.*;
import ${package.Entity}.*;
import cn.hutool.core.bean.BeanUtil;
import com.yingxin.yx.framework.commons.id.IdHelper;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.ArrayList;

/**
 *
 * @author ${author}
 * @since ${date}
 */
<#if kotlin>
class ${Builder}
<#else>
public class ${Builder} {

    <#if buildType == 'BILL'>
    /**
    * build
    * @param vo
    * @return
    */
    public static ${entity} build(${CreateVo} vo) {
        ${entity} ${billClassVariable} = new ${entity}();
        BeanUtil.copyProperties(vo, ${billClassVariable});
        if (StringUtils.isEmpty(${billClassVariable}.getId())) {
            ${billClassVariable}.setId(IdHelper.snowflakeId());
        }
        return ${billClassVariable};
    }
    <#elseif buildType == 'ITEM'>
    /**
    * build
    * @param vo
    * @return
    */
    public static ${entity} build(${CreateVo} vo) {
        ${entity} ${itemClassVariable} = new ${entity}();
        BeanUtil.copyProperties(vo, ${itemClassVariable});
        if (StringUtils.isEmpty(${itemClassVariable}.getId())) {
            ${itemClassVariable}.setId(IdHelper.snowflakeId());
        }
        return ${itemClassVariable};
    }
    <#else>
    /**
    * build
    * @param vo
    * @return
    */
    public static ${entity} build(${CreateVo} vo) {
        ${entity} ${entityVariable} = new ${entity}();
        BeanUtil.copyProperties(vo, ${entityVariable});
        return ${entityVariable};
    }
    </#if>
    <#if buildType == 'BILL' || buildType == 'ITEM'>

    /**
    * build
    * @param vo
    * @return
    */
    public static List<${itemClass}> buildItems(${billClass} ${billClassVariable}, ${billClass}CreateVo vo) {
        // 合同明细
        List<${itemClass}> items = new ArrayList<>();
        for (${billClass}CreateVo.Inner${billClass}CreateVoItem voItem : vo.getItems()) {
            ${itemClass} item = new ${itemClass}();
            BeanUtil.copyProperties(voItem, item);
            // 没有id是新增，有id是修改
            if (StringUtils.isEmpty(item.getId())) {
                item.setId(IdHelper.snowflakeId());
            }
            item.set${firstLetterUpperForeignProperty}(${billClassVariable}.getId());
            items.add(item);
        }
        // 数量
        ${billClassVariable}.setQty(items.size());
        return items;
    }

    </#if>
}
</#if>
